package start.demo01.datatype.json.entity.editor.jsonEditor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import start.demo01.datatype.json.interfaces.JSONEditor;

import java.util.Iterator;
import java.util.Map;

//创建sql
public class JSONSQLEditor extends JSONEditor {


    //Object sql()
    public JSONSQLEditor(String args) {
        super(args);
    }
    {
        name = "SQL";
    }

    @Override
    protected void operationObject(JSONObject object, String key) {
        StringBuilder builder = new StringBuilder();
        StringBuilder values = new StringBuilder();
        //insert into zero(ip,type,opera_time,device_name,time,group_path,device)
        //values('ip','type','2022-12-19 12:00:00','device_name','time','group_path','za');
        builder.append("insert into ").append("?").append('(');
        Iterator<Map.Entry<String, Object>> iterator = object.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            builder.append(entry.getKey()).append(',');
            Object value = entry.getValue();
            if (value instanceof String)
                values.append('\'').append(value).append('\'');
            else
                values.append(value);
            values.append(',');
        }
        builder.deleteCharAt(builder.length() - 1)
                .append(") values(")
                .append(values.deleteCharAt(values.length() - 1)).append(')');
        map.put("sql", builder.toString());
    }

    @Override
    protected void operationArray(JSONArray array, int index) {
        return;
    }

    @Override
    protected void operation(JSONObject object) {

    }
}
